﻿local L = LibStub( "AceLocale-3.0" ):GetLocale( "SimpleActionSets" )

function SimpleActionSets:ActionButton_OnClick( self, button )
	-- Pick up action, and replace with held action (if there is one)
	local bar = self:GetParent():GetID();
	local id = self:GetID();
	local LocalSavedAction;
	local SAS_SavedPickup = SimpleActionSets:GetSavedPickup();
	local SAS_ReturnAction = SimpleActionSets:GetReturnAction();
	local inCombat = SimpleActionSets:GetCombatStatus();
	local SAS_TempButton = SimpleActionSets:GetTemp(bar, id);

	if ( button == "RightButton" ) then
		if ( SASFakeDragFrame.Action ) then
			SimpleActionSets:FakeDrag_Drop(1);
		elseif ( SAS_TempButton == 0 ) then
			SimpleActionSets:SetTemp(bar, id, 1);
		elseif ( SAS_TempButton == 1 or SAS_TempButton) then
			SimpleActionSets:SetTemp(bar, id, nil);
		else
			SimpleActionSets:SetTemp(bar, id, 0);
		end
		SimpleActionSets:Actions_UpdateAction( bar, id );
	elseif ( IsShiftKeyDown() and not IsControlKeyDown() and not SASFakeDragFrame.Action ) then
		LocalSavedAction = SAS_TempButton;
		ClearCursor();
	else
		if ( SAS_SavedPickup ) then
			-- Cursor has an item / action / macro that can be placed on the action bar
			SimpleActionSets:Debug("SASActionButton_OnClick getting action "..SimpleActionSets:ParseActionInfo(SAS_SavedPickup, 1).." from cursor");
			if ( SimpleActionSets:GetTemp(bar) and not SAS_TempButton == SAS_SavedPickup ) then
				LocalSavedAction = SAS_TempButton;
			end
			SimpleActionSets:SetTemp(bar, id, SAS_SavedPickup);
			SimpleActionSets:Actions_UpdateAction( bar, id );
			if ( SAS_ReturnAction and not inCombat ) then
				PlaceAction( SAS_ReturnAction );
			elseif ( not inCombat ) then
				ClearCursor();
			end
		elseif ( SASFakeDragFrame.Action ) then
			-- Cursor fake drag is holding an action
			SimpleActionSets:Debug("SASActionButton_OnClick getting fake drag action "..SimpleActionSets:ParseActionInfo(SASFakeDragFrame.Action, 1));
			if ( SimpleActionSets:GetTemp(bar) and SAS_TempButton ) then
				LocalSavedAction = SAS_TempButton;
			end
			SimpleActionSets:SetTemp(bar, id, SimpleActionSets:FakeDrag_Drop(1));
			SimpleActionSets:Actions_UpdateAction( bar, id );
		elseif ( SAS_TempButton ) then
			SimpleActionSets:Debug("SASActionButton_OnClick putting action "..SimpleActionSets:ParseActionInfo(SAS_TempButton, 1).." on fake drag");
			-- This slot already has an action, pick it up
			LocalSavedAction = SAS_TempButton;
			SimpleActionSets:SetTemp(bar, id, nil);
			SimpleActionSets:Actions_UpdateAction( bar, id );
			if ( IsControlKeyDown() and not IsShiftKeyDown() ) then
				LocalSavedAction = nil;
			end
			_G[self:GetName().."Delete"]:Hide();
			_G[self:GetName().."Copy"]:Hide();
		end
	end

	if ( LocalSavedAction ) then
		SimpleActionSets:FakeDrag_PickupAction( LocalSavedAction );
	end
	SimpleActionSets:Actions_SaveEnable();
	SimpleActionSets:ActionButton_OnEnter(self);
end

function SimpleActionSets:ActionButton_OnEnter(self)
	-- Make Tooltip to display for this button
	self:SetScript("OnUpdate", function(self) SimpleActionSets:ActionButton_OnUpdate(self) end); --this.hasfocus = 1;

	local bar = self:GetParent():GetID();
	local id = self:GetID();
	local enabled = _G["SASActionBar"..bar.."Enable"]:GetChecked();

	if ( not enabled ) then
		SetDesaturation( _G[self:GetName().."Icon"], 0 );
		self:SetAlpha(1.0);
		self:SetNormalTexture("Interface\\Buttons\\UI-Quickslot");
	end

	if ( _G[self:GetName().."Icon"]:IsVisible() and SimpleActionSets:GetTemp() ) then
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
		SimpleActionSets:SetTooltip( self, bar, id );
	else
		GameTooltip:Hide();
	end
end

function SimpleActionSets:ActionButton_OnLeave(self)
	-- Hide the tooltip, reset desaturated on disabled bars
	local enabled = _G["SASActionBar"..self:GetParent():GetID().."Enable"]:GetChecked();
	local bar = self:GetParent():GetID();
	local id = self:GetID();
	SimpleActionSets:Actions_UpdateAction( bar, id );
	self:SetScript("OnUpdate", nil); --this.hasfocus = nil;
	_G[self:GetName().."Delete"]:Hide();
	_G[self:GetName().."Copy"]:Hide();
	GameTooltip:Hide();
end

function SimpleActionSets:ActionButton_OnDragStart(self)
	local bar = self:GetParent():GetID();
	local id = self:GetID();

	SimpleActionSets:FakeDrag_PickupAction( SimpleActionSets:GetTemp( bar, id) );
	if ( not IsShiftKeyDown() ) then
		SimpleActionSets:SetTemp(bar, id, nil);
		SimpleActionSets:Actions_UpdateAction( bar, id );
		SimpleActionSets:Actions_SaveEnable(self);
	end
end

function SimpleActionSets:ActionButton_OnDragStop(self)
	SimpleActionSets:FakeDrag_Drop();
end

function SimpleActionSets:ActionButton_OnReceiveDrag(self)
	SimpleActionSets:Debug("Drag Receive");
	SimpleActionSets:ActionButton_OnClick(self);
end

function SimpleActionSets:ActionButton_OnMouseDown( self, button )
	if ( button == "RightButton" ) then
		local bar = self:GetParent():GetID();
		local id = self:GetID();
		local SAS_TempBar = SimpleActionSets:GetTemp(bar);
		if ( SAS_TempBar and SAS_TempBar[id] and type(SAS_TempBar[id]) == "string" ) then
			self.mousedown = 1;
		end
	end
end

function SimpleActionSets:ActionButton_OnMouseUp( self )
	self.mousedown = nil;
end

function SimpleActionSets:ActionButton_OnUpdate(self)
	if ( not _G[self:GetName().."Icon"]:IsVisible() or not SimpleActionSets:GetTemp() ) then return; end

	if ( self.mousedown or ( IsControlKeyDown() and not IsShiftKeyDown() ) ) then
		_G[self:GetName().."Delete"]:Show();
	else
		_G[self:GetName().."Delete"]:Hide();
	end
	if ( IsShiftKeyDown() and not IsControlKeyDown() ) then
		_G[self:GetName().."Copy"]:Show();
	else
		_G[self:GetName().."Copy"]:Hide();
	end
end